import os
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

def generate_secure_key():
    # 使用密钥派生函数
    salt = os.urandom(16)
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=100000,
    )

    password = b"user-provided-password"
    key = kdf.derive(password)
    return key, salt

# 使用示例
if __name__ == "__main__":
    key, salt = generate_secure_key()
    print("Key:", key)
    print("Salt:", salt)
